package com.zhou.juc.test02_synchronized;

/**
 * @author zhouyanxiang
 * @create 2020-08-2020/8/11-18:30
 */
public class SaleTickets {

    public static void main(String[] args) {
        T t = new T();
        for (int i = 0; i < 10; i++) {
           new Thread(t).start();
        }
    }

}
class T implements Runnable{
    // 定义售票数量
    private static int tickets = 1000;

    @Override
    public void run() {
        while (true){
            // 锁住当前对象
            synchronized (this){
                if (tickets > 0){
                    tickets--;
                    System.out.println(Thread.currentThread().getName() + " 正在售卖第 " + tickets + " 张票 ");
                }
                if (tickets == 0){
                    break;
                }
            }

            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}